/**
 * 
 */
package com.cuc.platform.core.componet.security.dao.mybatis;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.support.SqlSessionDaoSupport;

import com.cuc.platform.core.componet.security.dao.SecurityComplexQueryDao;
import com.cuc.platform.core.componet.security.domain.SecurityAuthority;
import com.cuc.platform.core.componet.security.domain.SecurityMenu;
import com.cuc.platform.core.componet.security.domain.SecurityResource;
import com.cuc.platform.core.componet.security.domain.SecurityRole;
import com.cuc.platform.core.componet.security.domain.SecurityUser;

/**
 * @author peepul
 * 安全模块的组合查询Dao实现类 
 *
 * @see SecurityComplexQuery.xml
 */
public class SecurityComplexQueryDaoImpl extends SqlSessionDaoSupport implements SecurityComplexQueryDao{
	
	/* (non-Javadoc)
	 * @see com.cuc.platform.componetServices.security.SecurityComplexQueryDao#queryUsers(java.util.Map)
	 */
	@Override
	public List<SecurityUser> queryUsers(Map<String, Object> map) {
		Map<String, Object> queryMap = new HashMap<String, Object>();
		
		/*
		 * 拼接查询字段
		 */
		//账号
		String userName = (String)map.get("userName");
		if((null != userName) && (!userName.equals(""))) {
			userName = "%" + userName + "%";
			queryMap.put("userName", userName);		
		}
		
		//用户名
		String aliasName = (String)map.get("aliasName");
		if((null != aliasName) && (!aliasName.equals(""))) {
			aliasName = "%" + aliasName + "%";
			queryMap.put("aliasName", aliasName);		
		}
		
		//用户状态 1、有效 0 无效
		Object status = (Object)map.get("status");
		if((null != status) && (!status.equals(""))) {
			queryMap.put("status", status);		
		}
		
		//执行查询操作
		SqlSession session = this.getSqlSession();
		SecurityComplexQueryDao dao = session.getMapper(SecurityComplexQueryDao.class);
		
		return dao.queryUsers(queryMap);
	}

	/* (non-Javadoc)
	 * @see com.cuc.platform.componetServices.security.SecurityComplexQueryDao#queryRoles(java.util.Map)
	 */
	@Override
	public List<SecurityRole> queryRoles(Map<String, String> map) {
		Map<String, String> queryMap = new HashMap<String, String>();		
		
		/*
		 * 拼接查询字段
		 */
		//角色名称
		String name = (String)map.get("name");
		if((null != name) && (!name.equals(""))) {
			name = "%" + name + "%";
			queryMap.put("name", name);		
		}
		
		//执行查询操作
		SqlSession session = this.getSqlSession();
		SecurityComplexQueryDao dao = session.getMapper(SecurityComplexQueryDao.class);
		
		return dao.queryRoles(queryMap);
	}

	/* (non-Javadoc)
	 * @see com.cuc.platform.componetServices.security.SecurityComplexQueryDao#queryAuths(java.util.Map)
	 */
	@Override
	public List<SecurityAuthority> queryAuths(Map<String, String> map) {
		Map<String, String> queryMap = new HashMap<String, String>();		
		
		/*
		 * 拼接查询字段
		 */
		//权限名称
		String name = (String)map.get("name");
		if((null != name) && (!name.equals(""))) {
			name = "%" + name + "%";
			queryMap.put("name", name);		
		}
		
		//执行查询操作
		SqlSession session = this.getSqlSession();
		SecurityComplexQueryDao dao = session.getMapper(SecurityComplexQueryDao.class);
		
		return dao.queryAuths(queryMap);
	}

	/**
	 * 系统资源查询
	 * 
	 * 拼接条件：name, res_type, res_string
	 */
	public List<SecurityResource> queryRescs(Map<String, String> map){
		
		Map<String, String> queryMap = new HashMap<String, String>();
		
		/*
		 * 拼接查询字段
		 */
		String name = map.get("name");
		if((null != name) && (!name.equals(""))) {
			name = "%" + name + "%";
			queryMap.put("name", name);
		}
		
		String res_type = map.get("res_type");
		if((null != res_type) && (!res_type.equals(""))) {
			queryMap.put("res_string", res_type);
		}
		
		String res_string = map.get("res_string");
		if((null != res_string) && (!res_string.equals(""))) {
			res_string = "%" + res_string + "%";
			queryMap.put("res_string", res_string);
		}
		
		//执行查询操作
		SqlSession session = this.getSqlSession();
		SecurityComplexQueryDao dao = session.getMapper(SecurityComplexQueryDao.class);
		
		return dao.queryRescs(queryMap);
	}	
	
	/* (non-Javadoc)
	 * @see com.cuc.platform.componetServices.security.SecurityComplexQueryDao#queryMenus(java.util.Map)
	 */
	@Override
	public List<SecurityMenu> queryMenus(Map<String, Object> map) {
		Map<String, Object> queryMap = new HashMap<String, Object>();
		
		/*
		 * 拼接查询字段
		 */
		//菜单名称
		String name = (String)map.get("name");
		if((null != name) && (!name.equals(""))) {
			name = "%" + name + "%";
			queryMap.put("name", name);		
		}
		
		//菜单名称
		String menuUrl = (String)map.get("menuUrl");
		if((null != menuUrl) && (!menuUrl.equals(""))) {
			menuUrl = "%" + menuUrl + "%";
			queryMap.put("menuUrl", menuUrl);		
		}
		
		//父菜单id
		Object parentId = (Object)map.get("parentId");
		if((null != parentId) && (!parentId.equals(""))) {
			queryMap.put("parentId", parentId);		
		}
		
		//层次级别
		Object level = (Object)map.get("level");
		if((null != level) && (!level.equals(""))) {
			queryMap.put("level", level);		
		}
		
		//菜单编码
		String code = (String)map.get("code");
		if((null != code) && (!code.equals(""))) {
			name = "%" + code + "%";
			queryMap.put("code", code);		
		}
		
		//执行查询操作
		SqlSession session = this.getSqlSession();
		SecurityComplexQueryDao dao = session.getMapper(SecurityComplexQueryDao.class);
		
		return dao.queryMenus(queryMap);
	}

	@Override
	public List<SecurityUser> queryUsersWithPage(Map<String, Object> map,
			int offset, int limit) {
		Map<String, Object> queryMap = new HashMap<String, Object>();
		
		/*
		 * 拼接查询字段
		 */
		//账号
		String userName = (String)map.get("userName");
		if((null != userName) && (!userName.equals(""))) {
			userName = "%" + userName + "%";
			queryMap.put("userName", userName);		
		}
		
		//用户名
		String aliasName = (String)map.get("aliasName");
		if((null != aliasName) && (!aliasName.equals(""))) {
			aliasName = "%" + aliasName + "%";
			queryMap.put("aliasName", aliasName);		
		}
		
		//用户状态 1、有效 0 无效
		Object status = (Object)map.get("status");
		if((null != status) && (!status.equals(""))) {
			queryMap.put("status", status);		
		}
		
		//执行查询操作
		SqlSession session = this.getSqlSession();
		
		List<SecurityUser> list = session.selectList(
				"queryUsers", 
				queryMap, new RowBounds(offset, limit));
		
		
		return list;
	}
}
